/* ====================================================================
 * (#)Header: JmsMessenger.java
 * (#)Revision: 
 * (#)Date Created: 02-Nov-09 10:32:15 AM
 * (#)Created By: Karol Wozniak
 * ====================================================================
 *
 * Copyright (c) 2007 karolak .  All rights reserved.
 *
 * ====================================================================
 * 
 * The software application represented here is the sole property of 
 * karolak. Its unauthorized utilization is expressly prohibited.
 * 
 * ====================================================================
 */
package com.karolak.jms;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;

/**
 * <code>JmsMessenger</code> is used to ....
 * 
 * @author <a href="mailto:kwozniak@hotmail.com">Karol Wozniak</a>
 */
public class JmsReceiver extends JmsBase {

	private MessageConsumer consumer;

	public JmsReceiver(String destinationJndiName) {
		super(destinationJndiName);
		try {
			setConsumer(getSession().createConsumer(getDestination()));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public JmsReceiver(Connection connection, String destinationJndiName) {
		super(connection, destinationJndiName);
		try {
			setConsumer(getSession().createConsumer(getDestination()));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}	
	

	public Message receiveMessage() throws JMSException {
		Message message = getConsumer().receive();
		return message;
	}

	@Override
	public void close() {
		if (getConsumer() != null) {
			try {
				getConsumer().close();
			} catch (Exception e) {
			}
		}
		super.close();
	}

	public MessageConsumer getConsumer() {
		return this.consumer;
	}

	public void setConsumer(MessageConsumer consumer) {
		this.consumer = consumer;
	}

	public void setMessageListener(MessageListener messageListener) {
		try {
			getConsumer().setMessageListener(messageListener);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
